<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

if(!defined('JFX_SYSTEM_START')) return;

define('JFX_CMS_RENDER', '1');

$VIEW->template = 'cms-default.tpl';
//$VIEW->addJsLink('flowplayer-3.0.6.min.js');


/*
if($DB->countRows($CONFIG->dbprefix.'cache', "id = '{$workspaceid}--{$pageid}'")>0){
    $VIEW->addBodyText($DB->oneValue($CONFIG->dbprefix.'cache', 'v', "id = '{$workspaceid}--{$pageid}'"));
    return;
}
*/
/*
if(file_exists($CONFIG->cacheDir.'/'.$workspaceid.'--'.$pageid.'.cache') &&
    is_readable(file_exists($CONFIG->cacheDir.'/'.$workspaceid.'--'.$pageid.'.cache'))
){
    $VIEW->addBodyText(file_get_contents($CONFIG->cacheDir.'/'.$workspaceid.'-jfxpage-'.$pageid.'.cache'));
    return;
}
*/


//$VIEW->addCssLink('admin.css');
//$DB->showOutput(true);
//$DB->debug(true);
$LINK = JFX::registry('JFX_Link');
//$DB->debug(false);
//$DB->showOutput(false);

if(!isset($workspaceid) || $workspaceid == '')
    if($USER->workspace > 0 && $USER->isAdmin()) $workspaceid = $USER->workspace;
    else $workspaceid = 1;

$USER->workspace = $workspaceid;

$pageid = (int) $LINK->getPageId();

if($pageid > 0){

    $SMARTY->assign('pageid', $pageid);

    $pData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}pages WHERE id = '{$pageid}'");
    if($pData['mirror_id'] != 0){
        // we are mirroring another page
        $pageid = $pData['mirror_id'];
        $pData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}pages WHERE id = '{$pageid}'");
    }

    // now we have a valid page id
    $JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After discovering the page id ('.$pageid.')');

    $VIEW->setTitle($LANG->getContent('core', 'heading', $pageid, '', $workspaceid));

    $pageContent = JFX::getPageContent($pageid, $workspaceid);

    $VIEW->addBodyText($pageContent);

}else{
    if(!JFX_Module::isInstalled($SETTINGS->get('module-search'))){
        $searchForm = '<p>'.$this->lang('search_not_enabled').'</p>';
    }else{
        $searchModule = JFX_Module::getInstance($SETTINGS->get('module-search'));
        $searchForm = $searchModule->getAdvancedSearchForm();
    }
    $SMARTY->assign('searchForm', $searchForm);
    $VIEW->template = '404.tpl';
}


